package cn.edu.zut.hkxy.service.impl;

import cn.edu.zut.hkxy.mapper.AdminMapper;
import cn.edu.zut.hkxy.mapper.NewsMapper;
import cn.edu.zut.hkxy.pojo.Admin;
import cn.edu.zut.hkxy.pojo.AdminExample;
import cn.edu.zut.hkxy.pojo.News;
import cn.edu.zut.hkxy.pojo.NewsExample;
import cn.edu.zut.hkxy.service.NewsService;
import cn.edu.zut.hkxy.service.UsersService;
import cn.edu.zut.hkxy.util.DateFormat;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Integer addAdmin(Admin admin) {
        return adminMapper.insert(admin);
    }

    @Override
    public Admin getAdminById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Admin> getAdminPage(AdminExample example, int pageNum) {
        example.setOrderByClause("id desc");
        //分页：第pageNum页，每页10条数据
        PageHelper.startPage(pageNum,10);
        List<Admin> list=adminMapper.selectByExample(example);
        return list;
    }

    @Override
    public boolean deleteById(Integer id) {
        int i=adminMapper.deleteByPrimaryKey(id);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        AdminExample example=new AdminExample();
        example.createCriteria().andIdIn(ids);
        return adminMapper.deleteByExample(example);
    }

    @Override
    public boolean modifyAdmin(Admin admin) {
        int i=adminMapper.updateByPrimaryKey(admin);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        int i=adminMapper.updateByPrimaryKeySelective(admin);
        if(i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean findUsername(String username) {
        AdminExample example=new AdminExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<Admin> admin=adminMapper.selectByExample(example);
        if(admin.size()==1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Admin usernameLogin(String username, String password) {
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andPasswordEqualTo(password);
        criteria.andUsernameEqualTo(username);
        List<Admin> admin=adminMapper.selectByExample(example);
        if (admin.size()==0) {
            return null;
        }else {
            return admin.get(0);
        }
    }
}
